﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading;

namespace ZBJF.Warranty.Common.Common
{
    /// <summary>
    /// Summary description for MailHelper.
    /// </summary>
    public class MailHelper
    {
        private readonly string _smtpServer;
        private readonly string _account;
        private readonly string _password;
        private readonly string _mainFrom;
        private readonly string _mailFromName;
        private string _subject;
        private string _body;
        private string _to;
        private string _cc;
        public bool? IsBodyHtml { get; set; }
        public List<System.Web.Mail.MailAttachment> Attachments { get; set; }
        /// <summary>
        /// CSS文件
        /// </summary>
        public string MailCss { get; set; }
        public string DebugMail
        {
            get;
            set;
        }
        public MailHelper()
        {
            _smtpServer = ConfigurationManager.AppSettings["SMTPServer"];
            _account = ConfigurationManager.AppSettings["SMTPUser"];
            _password = ConfigurationManager.AppSettings["SMTPPasssword"];
            _mainFrom = ConfigurationManager.AppSettings["MailFrom"];
            _mailFromName = ConfigurationManager.AppSettings["MailFromName"];
            DebugMail = ConfigurationManager.AppSettings["MailToForDebug"];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="to"></param>
        /// <param name="cc"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public bool SendMail(string to, string cc, string subject, string body)
        {
            _subject = subject;
            _body = MailCss + body;
            _to = to;
            _cc = cc;

            Thread thread = new Thread(new ThreadStart(SendMail));
            thread.Start();

            return true;
        }
        private void SendMail()
        {
            System.Web.Mail.MailMessage mail1 = new System.Web.Mail.MailMessage();
            try
            {
                mail1.To = _to;
                mail1.From = string.Format("\"{0}\"<{1}>", _mailFromName, _mainFrom);
                mail1.Subject = _subject;
                mail1.BodyFormat = System.Web.Mail.MailFormat.Html;
                mail1.Body = _body;
                mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
                mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _mainFrom); //set your username here
                mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password); //set your password here
                mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//set port
                mail1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//set is ssl               
                System.Web.Mail.SmtpMail.SmtpServer = _smtpServer;

                var existAttachments = Attachments != null && Attachments.Any() && Attachments.Count > 0;
                if (existAttachments)
                {
                    for (int i = 0; i < Attachments.Count; i++)
                    {
                        var item = Attachments[i];
                        mail1.Attachments.Add(item);
                    }
                }

                System.Web.Mail.SmtpMail.Send(mail1);

                if (existAttachments)
                {
                    Attachments.Clear();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(string.Format("_smtpServer{0}_account{1},_password{2},_mainFrom{3}", _smtpServer, _account, _password, _mainFrom) + ex.ToString(), LogType.error);
            }
        }
    }
}
